/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7

Rectangle {
    id: comboboxid
    width: 100
    height: 24
    border.color: "#000000"


    property int fontpixelsize: 14
    property bool opened: false
    property alias deletebutton: listdeleteerrect.visible
    property int selectedind: -1

    signal itemSelected(string label)
    signal itemDelete(string label)

    function setSelectedItem(label)
    {
        selectedind = -1

        for (var i = 0; i < items.count; i++)
        {
            if (items.get(i).label == label)
            {
                selectedind = i
                selectiontextid.text = label
            }
        }
    }

    function getSelectedLabel()
    {
        return selectiontextid.text
    }

    function deleteComboItem(index)
    {
        if (index > -1 && index < items.count)
        {
            items.remove(index)
            if (items.count > 0)
            {
                setSelectedItem(items.get(0).label)

            }
            else
            {
                selectedind = -1
                selectiontextid.text = ""
            }
        }
    }

    function setComboItems(itemarray)
    {
        var menuitemstr = ""
        var i = 0
        //menuitemModel.clear()
        while (items.count > 0)
        {
            items.remove(0)
        }

        for (i=0; i<itemarray.length;i++)
        {
            items.append({"label":itemarray[i]})
        }

        if (itemarray.length > 0)
        {
            setSelectedItem(items.get(0).label)
        }
        else
        {
            selectedind = -1
            selectiontextid.text = ""
        }

    }

    function addComboItem(label)
    {
        items.append({"label":label})
    }

    ListModel {
        id: items
    }

    Component {
        id: listItem
        Rectangle {
            id: itemrect
            color: "white"
            x: 1
            y: 1
            clip: true

            property string itemlabel: label
            property int itemindex: index

            width: itemsrect.width-2*comboboxid.border.width
            height: comboboxid.height-2*comboboxid.border.width

            MouseArea {
                id: mousearea
                anchors.fill: parent
                hoverEnabled: true

                onClicked:
                {
                    selectiontextid.text = itemlabel
                    selectedind = itemindex
                    itemsrect.visible = false
                    comboboxid.itemSelected(itemlabel)
                }

                states: [
                State {
                    id: stateHovered
                    name: "hovered"; when: mousearea.containsMouse
                    PropertyChanges { target: itemrect; color: "yellow"; }
                }
                ]
            }

            Text {
                anchors.verticalCenter: parent.verticalCenter
                anchors.left:  parent.left
                anchors.leftMargin: 3
                font.pixelSize: comboboxid.fontpixelsize
                text: itemlabel
            }

        }
    }

    Rectangle {
        id: itemsrect
        width: selectiontextid.width
        height: comboboxid.height * (items.count > 5?5:items.count) - 2*comboboxid.border.width
        clip: true
        border.color: "#000000"
        x: 0
        y: comboboxid.height+2*comboboxid.border.width


        onVisibleChanged:
        {
            itemsrect.focus = (itemsrect.visible?true:false)
            comboboxid.z = (itemsrect.visible?5:0)
        }

        onActiveFocusChanged:
        {
            itemsrect.visible = itemsrect.focus
        }

        visible:  false

        ListView {
            id: itemslist
            model: items
            delegate: listItem
            anchors.fill: parent
            snapMode: ListView.SnapOneItem


        }
    }


    Text {
        id: selectiontextid
        anchors.verticalCenter: parent.verticalCenter
        anchors.left:  parent.left
        anchors.leftMargin: 3
        width: comboboxid.width - listopenerrect.width-2*comboboxid.border.width
        height: comboboxid.fontpixelsize
        text: ""
        font.pixelSize: comboboxid.fontpixelsize

        MouseArea {
            id: mouseareatext
            anchors.fill: parent

            onClicked:
            {
                itemsrect.visible = !itemsrect.visible
            }


        }
    }

    property Gradient gradientPressed: Gradient {
        GradientStop { position: 0.46; color: "#397191"; }
        GradientStop { position: 1; color: "#000000"; }}

    property Gradient gradientReleased: Gradient {
        GradientStop { position: 0.46; color: "#87cefa"; }
        GradientStop { position: 1; color: "#000000"; }}

    property Gradient gradientDeletePressed: Gradient {
        GradientStop { position: 0.46; color: "#993131"; }
        GradientStop { position: 1; color: "#000000"; }}

    property Gradient gradientDeleteReleased: Gradient {
        GradientStop { position: 0.46; color: "#c86b6b"; }
        GradientStop { position: 1; color: "#000000"; }}

    Rectangle {
        id: listopenerrect
        width: 50
        transformOrigin: Item.TopLeft
        border.width: 0
        gradient: Gradient {
            GradientStop {
                position: 0.46
                color: "#87cefa"
            }

            GradientStop {
                position: 1
                color: "#000000"
            }
        }
        border.color: "#000000"
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0
        anchors.right: (listdeleteerrect.visible?listdeleteerrect.left:parent.right)
        anchors.rightMargin: 0


        Image {
            id: openarrow
            anchors.fill: parent
            width: 50
            height: 24
            fillMode: Image.PreserveAspectFit
            source: "cbarrow.gif"

            MouseArea {
                id: listopenermouseareaid
                anchors.fill: parent

                onPressed:
                {
                    listopenerrect.gradient = gradientPressed
                }

                onReleased:
                {
                    listopenerrect.gradient = gradientReleased
                }

                onClicked:
                {
                    itemsrect.visible = !itemsrect.visible
                }
            }
        }
    }

    Rectangle {
        id: listdeleteerrect
        width: 30
        transformOrigin: Item.TopLeft
        border.width: 0
        gradient: Gradient {
            GradientStop {
                position: 0.46
                color: "#c86b6b"
            }

            GradientStop {
                position: 1
                color: "#000000"
            }
        }
        border.color: "#000000"
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0
        anchors.right: parent.right
        anchors.rightMargin: 0


        Image {
            id: deletearrow
            anchors.fill: parent
            width: 40
            height: 24
            fillMode: Image.PreserveAspectFit
            source: "cbdelete.gif"

            MouseArea {
                id: listdeletemouseareaid
                anchors.fill: parent

                onPressed:
                {
                    listdeleteerrect.gradient = gradientDeletePressed
                }

                onReleased:
                {
                    listdeleteerrect.gradient = gradientDeleteReleased
                }

                onClicked:
                {
                    comboboxid.itemDelete(selectiontextid.text)
                }
            }
        }
    }


}
